// picker/picker.js
Component({
    options: {
        styleIsolation: 'isolated',
    },
    /**
     * 组件的属性列表
     */
    properties: {
        options: {
            type: Array,
            value: []
        },
        displayField: {
            type: String
        },
        valueField: {
            type: String
        },
        /**
         * "link": scroll间联动  "normal": scroll相互独立
         */
        scrollType: {
            type: String,
            value: 'normal'
        },
        listData: {
            type: Array,
            value: [],
            
        },
        isShowPicker: {
            type: Boolean,
            value: false,
            observer: function (newVal) {
                if (newVal) {
                    this._openPicker()
                } else {
                    this._closePicker()
                }
            }
        },
        cancelText: {// 取消按钮文案
            type: String,
            value: '取消'
        },
        confirmText: {// 确定按钮文案
            type: String,
            value: '确定'
        },
        title: {
            type: String
        },
        pickerHeaderStyle: String, // 标题栏样式 view
        sureStyle: String, // 标题栏确定样式  text
        cancelStyle: String, // 标题栏取消样式 text
        titleStyle: String, // 标题栏标题样式  view
        maskStyle: String, // 设置蒙层的样式（详见picker-view） view
        indicatorStyle: String, // 设置选择器中间选中框的样式（详见picker-view） view
        chooseItemTextStyle: String// 设置picker列表文案样式 text
    },

    /**
     * 组件的初始数据
     */
    data: {
        inner: {
            scrollEnd: true, // 滚动是否结束
            lastValue: [], // 上次各个colum的选择索引
            tempValue: [],
            isFirstOpen: true,
            onlyKey: '',
            defaultPickDataTemp: '',
            listDataTemp: ''
        },
        columnsData: [],
        value: [],
        backData: [],
        height: 0,
        isOpen: false,
        isUseKeywordOfShow: false,
        
    },
    /**
     * 组件的方法列表
     */
    methods: {
        tapModal() {
            this.properties.isShowPicker = false
            this.closePicker();
        },
        cancle() {
            this.triggerEvent('cancle')
            this.closePicker()
        },
        confirm() {
            const { scrollEnd, tempValue } = this.data
            if (!scrollEnd) return
            const backData = this._getBackDataFromValue(tempValue)
            this.setData({
                backData
            })
            this.triggerEvent('sure', {
                choosedData: backData,
                choosedIndexArr: tempValue
            })
            this.closePicker()
        },
        bindChange(e) {
            const { scrollType } = this.properties
            const { lastValue } = this.data
            let val = e.detail.value;
            switch (scrollType) {
                case 'normal':
                    this.data.tempValue = val.concat()
                    this.data.tempValue = val.concat()
                    break;
            }
        },
        bindpickend() {
            this.data.inner.scrollEnd = true
        },
        bindpickstart() {
            this.data.inner.scrollEnd = false
        },
        openPicker() {
            if (!this.data.isFirstOpen) {
                if (this.properties.listData.length !== 0) {
                    this._setDefault(this._computedBackData(this.data.backData))
                }
            }
            this.data.isFirstOpen = false
            this.setData({
                isOpen: true
            })
        },
        closePicker() {
            this.setData({
                isOpen: false
            })
        },
        getScrollCompareIndex(arr1, arr2) {
            let tempIndex = -1
            for (let i = 0, len = arr1.length; i < len; i++) {
                if (arr1[i] !== arr2[i]) {
                    tempIndex = i
                    break
                }
            }
            return tempIndex
        },
        // 根据id获取索引
        getIndexByIdOfObject(listData, idArr, key, arr) {
            if (!Array.isArray(listData)) return
            for (let i = 0, len = listData.length; i < len; i++) {
                if (listData[i][key] === idArr[arr.length][key]) {
                    arr.push(i)
                    return this._getIndexByIdOfObject(listData[i].children, idArr, key, arr)
                }
            }
        },
        setDefault(inBackData) {
            const { scrollType } = this.properties
            let { listData, defaultPickData } = this.properties

            const { lastValue } = this.data
            if (inBackData) {
                defaultPickData = inBackData
            }
            let backData = []
        },
        computedBackData(backData) {
            const { scrollType, listData } = this.properties
        },
        compareObj(o1, o2) {
            const { keyWordsOfShow } = this.properties
            if (typeof o1 !== 'object') {
                return o1 === o2
            } else {
                return o1[keyWordsOfShow] === o2[keyWordsOfShow]
            }
        },
        getBackDataFromValue(val) {
            let tempArr = []
            if (val.length > 0) {
                tempArr = this.data.columnsData.reduce((t, v, i) => {
                    return t.concat(v[val[i]])
                }, [])
            } else {
                tempArr = this.data.columnsData.map((v, i) => v[0])
            }
            return tempArr
        },
        compareDate() { // 完全相等返回true
            const { defaultPickDataTemp, listDataTemp } = this.data
            const { defaultPickData, listData } = this.properties

            return defaultPickDataTemp === defaultPickData && listDataTemp === listData
        },
        setTempData() {
            const { defaultPickData, listData } = this.properties
            this.data.defaultPickDataTemp = defaultPickData
            this.data.listDataTemp = listData
        }
    },

    observers: {
        options: function (newVal: any) {
            if (newVal.length === 0 || this._compareDate()) return
            this._setTempData()
            this._setDefault()
        }
    }
})
